博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net core系列 47 Identity 自定义用户数据
阅读量:6258 次
发布时间:2019-06-22

本文共 7965 字,大约阅读时间需要 26 分钟。

一.概述

         接着上篇的WebAppIdentityDemo项目,将自定义用户数据添加到Identity DB,自定义扩展的用户数据类应继承IdentityUser类, 文件名为Areas / Identity / Data / {项目名称}User.cs。自定义的用户数据模型属性需要使用[PersonalData]来修饰,以便自动下载和删除。使数据能够下载和删除有助于满足GDPR要求。

 

  1.1 自定义用户数据类 WebAppIdentityDemo.Areas.Identity.Data。

public class WebAppIdentityDemoUser:IdentityUser    {        ///         /// Full name        ///         [PersonalData]        public string Name { get; set; }        ///         /// Birth Date        ///         [PersonalData]        public DateTime DOB { get; set; }    }

    使用属性修饰PersonalData特性是:

      使用Areas/Identity/Pages/Account/Manage/DeletePersonalData.cshtml 页调用UserManager.Delete

      使用Areas/Identity/Pages/Account/Manage/DownloadPersonalData.cshtml 页下载用户数据

 

  1.2 修改IdentityHostingStartup

    将Startup中有关identity的服务移到该文件中,好集中管理。

public class IdentityHostingStartup : IHostingStartup    {        public void Configure(IWebHostBuilder builder)        {            builder.ConfigureServices((context, services) => {                services.AddDbContext
(options => options.UseSqlServer( context.Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity
() .AddDefaultUI() .AddEntityFrameworkStores
(); services.Configure
(options => { // Password settings. options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequireUppercase = true; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; // Lockout settings. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); options.Lockout.MaxFailedAccessAttempts = 5; options.Lockout.AllowedForNewUsers = true; // User settings. options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = false; }); services.ConfigureApplicationCookie(options => { // Cookie settings options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); options.LoginPath = $"/Identity/Account/Login"; options.LogoutPath = $"/Identity/Account/Logout"; options.AccessDeniedPath = $"/Identity/Account/AccessDenied"; }); }); } }

 

  1.3 修改DbContext上下文

// public class ApplicationDbContext : IdentityDbContext    public class ApplicationDbContext : IdentityDbContext
{ public ApplicationDbContext(DbContextOptions
options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); // Customize the ASP.NET Core Identity model and override the defaults if needed. // For example, you can rename the ASP.NET Core Identity table names and more. // Add your customizations after calling base.OnModelCreating(builder); } }

 

  1.4 数据库版本迁移

  PM> Add-Migration IdentitySchema2  PM> Update-Database IdentitySchema2

  1.5 更新注册页面

    在开发中,会员系统要重用修改的页面,把IdentityUser 改成WebAppIdentityDemoUser。如果没改将报错:未解析到服务IdentityUser。红色部分为修改的代码

[AllowAnonymous]    public class RegisterModel : PageModel    {        // private readonly SignInManager
_signInManager; //private readonly UserManager
_userManager; private readonly SignInManager
_signInManager; private readonly UserManager
_userManager; private readonly ILogger
_logger; private readonly IEmailSender _emailSender; public RegisterModel( UserManager
userManager, SignInManager
signInManager, ILogger
logger, IEmailSender emailSender) { _userManager = userManager; _signInManager = signInManager; _logger = logger; _emailSender = emailSender; } [BindProperty] public InputModel Input { get; set; } public string ReturnUrl { get; set; } public class InputModel { [Required] [DataType(DataType.Text)] [Display(Name = "Full name")] public string Name { get; set; } [Required] [Display(Name = "Birth Date")] [DataType(DataType.Date)] public DateTime DOB { get; set; } [Required] [EmailAddress] [Display(Name = "Email")] public string Email { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } } public void OnGet(string returnUrl = null) { ReturnUrl = returnUrl; } public async Task
OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { // var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; var user = new WebAppIdentityDemoUser { UserName = Input.Email, Email = Input.Email, Name = Input.Name, DOB = Input.DOB }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by
clicking here."); await _signInManager.SignInAsync(user, isPersistent: false); return LocalRedirect(returnUrl); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return Page(); } }

  

@page@model RegisterModel@{    ViewData["Title"] = "Register";}

@ViewData["Title"]

Create a new account.


@section Scripts {
}

    启动程序,运行注册页,保存成功后,查看数据库,如下所示:

 

  1.6 其它相关页修改

    下面这些最基本的页面,都需要把IdentityUser 改成WebAppIdentityDemoUser。

      登录页 /Identity/Account/Login

      注册页 Identity/Account/Register

      会员管理后台主页 /Identity/Account/Manage/index

      个人资料页 /Identity/Account/Manage/PersonalData

      个人资料删除 /Identity/Account/Manage/DeletePersonalData

      个人资料下载/Identity/Account/Manage/ DownloadPersonalData.cshtml

      分部页 _ManageNav.cshtml

  

  下面是 Account/Manage/Index.cshtml 页,已经加上自定义的二个用户字段信息,具体更新代码见官网

    下面是/Identity/Account/Manage/PersonalData.cshtml 页,已经可以下载和删除用户数据

 

   参考文献

    

  

转载于:https://www.cnblogs.com/MrHSR/p/10578369.html

你可能感兴趣的文章
Python系语言发展综述
查看>>
新手 开博
查看>>
借助开源工具高效完成Java应用的运行分析
查看>>
163 yum
查看>>
第三章:Shiro的配置——深入浅出学Shiro细粒度权限开发框架
查看>>
80后创业的经验谈(转,朴实但实用!推荐)
查看>>
让Windows图片查看器和windows资源管理器显示WebP格式
查看>>
我的友情链接
查看>>
vim使用点滴
查看>>
embedded linux学习中几个需要明确的概念
查看>>
mysql常用语法
查看>>
Morris ajax
查看>>
【Docker学习笔记(四)】通过Nginx镜像快速搭建静态网站
查看>>
ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
查看>>
<转>云主机配置OpenStack使用spice的方法
查看>>
java jvm GC 各个区内存参数设置
查看>>
[使用帮助] PHPCMS V9内容模块PC标签调用说明
查看>>
关于FreeBSD的CVSROOT的配置
查看>>
基于RBAC权限管理
查看>>
数学公式的英语读法
查看>>